package com.jt.www.common.controller;

import com.jt.www.common.service.SendEmailsService;
import com.jt.www.model.issue.entity.IssuePolicyLogEntity;
import com.jt.www.model.mail.dto.MailDTO;
import com.jt.www.model.reps.GenericResponse;
import com.jt.www.queue.issue.ElecApprovalMsglistener;
import com.jt.www.remote.PolClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;

/**
 * @Description:
 * @author: xuxinku
 * @Date: 2019/6/16 14:34
 * @ModifiedDate：
 * @Copyright:江泰保险股份有限公司
 */
@Api(value = "发送邮件接口", tags = {"Mail-INFO-API"}, description = "发送邮件接口")
@RestController
@RequestMapping(value = "/mail")
public class MailController {

    public static final Logger log = LoggerFactory.getLogger(MailController.class);


    @Autowired
    SendEmailsService sendEmailsService;
    @Autowired
    ElecApprovalMsglistener elecApprovalMsglistener;
    @Autowired
    private PolClient polClient;

    @ApiOperation(value = "发送邮件", httpMethod = "POST")
    @PostMapping(value = "/sendEmail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity sendEmail(@ApiParam(value = "邮件类型", required = true) @RequestParam(value = "type") String type,
                                    @ApiParam(value = "邮件参数", required = true) @RequestBody MailDTO mailDTO

    ) {

        //MailDTO mailDTO = new MailDTO();
        try {
            return sendEmailsService.sendEmail(mailDTO, type);
        } catch (Exception e) {
            e.printStackTrace();
            return GenericResponse.ng(e.getMessage());
        }

    }

    @ApiOperation(value = "测试", httpMethod = "POST")
    @PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity test(@ApiParam(value = "批单号", required = true) @RequestParam String applicationFormCode,
                               @ApiParam(value = "资源ID", required = true) @RequestParam String fileId) {

        //MailDTO mailDTO = new MailDTO();
        try {
            System.out.println("批单号" + applicationFormCode);
            System.out.println("资源ID" + fileId);
            IssuePolicyLogEntity issuePolicyLogEntity = new IssuePolicyLogEntity();
            elecApprovalMsglistener.bindingResources(applicationFormCode, fileId, issuePolicyLogEntity);
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return GenericResponse.ng(e.getMessage());
        }

    }


    @GetMapping(value = "/v1/queryEndorsementDetailByApplyNo", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String queryEndorsementDetailByApplyNo(
            @NotBlank(message = "批单申请号不能为空！") @RequestParam("applyNo") String applyNo) {

        String eduQueryEndorsementDetailVo = null;
        try {

            eduQueryEndorsementDetailVo = polClient.queryEndorsementDetailByApplyNo(applyNo);
        } catch (Exception e) {
            e.printStackTrace();
        }

//        JSONObject jsonObject = JSONObject.parseObject(eduQueryEndorsementDetailVo);
//        JSONObject data = jsonObject.getJSONObject("data");
//        System.out.println("data=====>"+data);
//        String applicationCode = data.getString("applicant");
//        String applyNo2 = data.getString("applyNo");
//        String endorsementsNo = data.getString("endorsementsNo");
//        JSONObject head =data.getJSONObject("modifyAfterHolder");
//        String linkEmail = head.getString("linkEmail");
//        String startDate = data.getString("startDate");
//        System.out.println("用户名"+applicationCode);
//        System.out.println("批单申请号"+applyNo2);
//        System.out.println("批单号"+endorsementsNo);
//        System.out.println("收件人邮箱"+linkEmail);
//        System.out.println("批单起期"+startDate);

        ElecApprovalMsglistener elecApprovalMsglistener = new ElecApprovalMsglistener();

        elecApprovalMsglistener.sendEmail(applyNo, "17", "20190516012");
        return eduQueryEndorsementDetailVo;
    }
}
